/**
 * 
 */
/**
 * @author Administrator
 *
 */
package com.feonix.common.view.grid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.feonix.common.util.FreemarkerUtil;
import com.feonix.common.util.StringUtil;

public class GridTag extends RequestContextAwareTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1564518639972730845L;

	private GridView gridView;
	private Map<String, Object> map = new HashMap<String, Object>();

	@Override
	protected int doStartTagInternal() throws Exception {
		this.gridView = ((GridView) this.pageContext.getRequest().getAttribute("grid"));
		createGridBody();
		this.gridView.clear();
		return 1;
	}

	private void createGridBody() {
		this.map.put("grid_id", this.gridView.id);
		this.map.put("grid_position", this.gridView.position);
		this.map.put("grid_checkbox_field", this.gridView.checkBoxField);
		createOptions();
		createHead();
		createToolsbar();
		createData();
		createPager();
		String str = FreemarkerUtil.processFTL("grid.ftl", this.map);
		try {
			this.pageContext.getOut().write(String.valueOf(str));
		} catch (Exception exception) {
			this.logger.warn("模板输出失败", exception);
		}
	}

	private void createOptions() {
		StringBuffer options = new StringBuffer();
		options.append("fitColumns:").append(this.gridView.fitColumns).append(",");
		options.append("striped:").append(this.gridView.striped).append(",");
		options.append("nowrap:").append(this.gridView.nowrap).append(",");
		options.append("pagination:").append(this.gridView.pagination).append(",");
		options.append("pagePosition:'").append(this.gridView.pagePosition).append("',");
		options.append("rownumbers:").append(this.gridView.rownumbers).append(",");
		options.append("singleSelect:").append(this.gridView.singleSelect).append(",");
		options.append("checkOnSelect:").append(this.gridView.checkOnSelect).append(",");
		options.append("selectOnCheck:").append(this.gridView.selectOnCheck).append(",");
		options.append("border:").append(this.gridView.border).append(",");
		options.append("queryParams:").append("{").append(StringUtils.join(this.gridView.queryParams.toArray(), ","))
				.append("},");
		options.append("onClickRow:").append("gridOnClickRow,");
		options.append("onSortColumn:").append("gridOnSortColumn,");

		options.deleteCharAt(options.length() - 1);
		String str = options.toString();
		this.map.put("grid_options", str);
		StringUtil.cleanStringBuffer(new StringBuffer[] { options });
	}

	private void createHead() {
		List<Head> map = this.gridView.heads;
		this.map.put("grid_heads", map);
	}

	private void createToolsbar() {
		List<Button> buttons = this.gridView.buttons;
		ArrayList<Button> hasRightButtons = new ArrayList<Button>();
		for (Button button : buttons) {
			if (button.hasRight()) {
				hasRightButtons.add(button);
			}
		}
		this.map.put("grid_buttons", hasRightButtons);
		this.map.put("grid_searchPlaceholder", this.gridView.searchPlaceholder);
		this.map.put("grid_searchText", StringUtil.getSafeString(this.gridView.searchText));
		this.map.put("grid_advsearch", Boolean.valueOf(this.gridView.showAdvSearch));
		this.map.put("grid_simplesearch", Boolean.valueOf(this.gridView.showSimpleSearch));
		this.map.put("grid_advWidth", Integer.valueOf(this.gridView.advDialogWidth));
		this.map.put("grid_advHeight", Integer.valueOf(this.gridView.advDialogHeight));
		this.map.put("grid_advOptions", this.gridView.advDialogOptions);

		this.map.put("grid_searchType", Integer.valueOf(this.gridView.searchType));
	}

	private void createData() {
		List<GridRow> list = this.gridView.datas;
		this.map.put("grid_datas", list);
	}

	private void createPager() {
		this.map.put("total", Integer.valueOf(this.gridView.total));
		this.map.put("pageSize", Integer.valueOf(this.gridView.pageSize));
		this.map.put("pageList", "[" + this.gridView.pageList + "]");
		this.map.put("pageNumber", Integer.valueOf(this.gridView.pageNumber));
		this.map.put("showPageList", Boolean.valueOf(this.gridView.showPageList));
		this.map.put("sortName", StringUtil.getString(this.gridView.sortName));
		this.map.put("sortOrder", StringUtil.getString(this.gridView.sortOrder));
	}

}